<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS.DS."Page.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS.DS."Page.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Page.php";

class PageController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function addAction()
    {   
    	//Create form enter info for new Page
    	$form = new Application_Form_Page();
    	
    	//Set label for the button
        $form->submit->setLabel('Add');
        
        //Set client view page form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập thông tin trang:</p>";
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) 
        {
        	//Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $name = $form->getValue('name');
                $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $parent_id = (int)$form->getValue('parent_id');
                $num_views     = 0;
				$language= $form->getValue('language');
                
                //Create new  Page object from datas
                $page = new Page(null, $name, $slug, $description, $content,0,0,$parent_id,$num_views, $created_date,null,0, $language);
                
                //Create  Page model 
                $Pages = new End_Models_Page();
                
                //Use insert function from  Page model
                $Pages->_insert($page);
                
                //Move to index page
                $this->_helper->redirector('index');
            } else 
            {
            	//Return form
                $form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $page_id = (int)$this->getRequest()->getPost('page_id');
                $Pages = new End_Models_Page();
                $Pages->_delete($page_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $page_id = $this->_getParam('page_id', 0);
            $Pages = new End_Models_Page();
            $this->view->Pages = Utility_Converter::objectToArray($Pages->_find($page_id));
        }
    }
    
    public function editAction()
    {
    	$page_id = $this->_getParam('page_id');
    	$form = new Application_Form_Page();
        $form->submit->setLabel('Save');
        $modified_date= date('Y-m-d H:i:s');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {	
               	$name = $form->getValue('name');
                $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $parent_id = (int)$form->getValue('parent_id');
				$language= $form->getValue('language');
   		
				$page = new Page($page_id, $name, $slug, $description, $content,0,0 ,$parent_id,0,null,$modified_date,0, $language);	
                $Pages = new End_Models_Page();						
                $Pages->_update($page);								
                $this->_helper->redirector('index');				
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $page_id = $this->_getParam('page_id', 0);
            if ($page_id > 0) 
            {
                $Pages = new End_Models_Page();
                $form->populate(Utility_Converter::objectToArray($Pages->_find($page_id)));
            }
        }
    }
    
    public function indexAction()
    {
    	$modelPage = new End_Models_Page();
		$page = $modelPage->fetchAll();
		$this->view->Page = $page;
    }
    
}







